
import sys
import pygtk
pygtk.require('2.0')
import gtk
import gobject

from Constants		import *



#####################################################################################################################################
# Debug class
class Debug:
	inst = None
	def __init__(self):
		sys.stdout = self
		sys.stderr = self

		w = gtk.Window()
		w.resize(400, 600)
		
		self.buffer = gtk.TextBuffer()
		text_view = gtk.TextView(buffer=self.buffer)
		scrolled_window = gtk.ScrolledWindow()
		scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
		scrolled_window.add(text_view)
		w.add(scrolled_window)
		w.show_all()
	
	@staticmethod
	def instance():
		if Debug.inst is None:
			Debug.inst = Debug()
		return Debug.inst
		
	def write(self, s):
		inst = Debug.instance()
		inst.buffer.insert_at_cursor('(dbg):'+str(s).strip() + '\n')
		

def DEBUG(s):
	if VISUAL_MODE and DEBUG_MODE:
		inst = Debug.instance()
		inst.write(s)
		return
	if DEBUG_MODE:
		sys.stdout.write('(dbg):'+str(s) + '\n')
